/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "LinearBrowser.h"
#include "Logger.h"
#include "Options.h"

LinearBrowser::LinearBrowser(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);

	QGridLayout* dictGrid = new QGridLayout(ui.dictionaryWidget);
	dictGrid->setContentsMargins(0, 0, 0, 0);
	dictGrid->addWidget(&mDictionary);

	QObject::connect(ui.nextWordButton, SIGNAL(clicked()), this, SLOT(browseNext()));
	QObject::connect(ui.previousWordButton, SIGNAL(clicked()), this, SLOT(browsePrevious()));
	QObject::connect(ui.wordNavSlider, SIGNAL(valueChanged(int)), this, SLOT(browseByOrder(int)));
	QObject::connect(ui.wordNavSpinBox, SIGNAL(valueChanged(int)), this, SLOT(browseByOrder(int)));
	QObject::connect(ui.browseInfo, SIGNAL(anchorClicked(const QUrl&)),
					this, SLOT(anchorClicked(const QUrl&)));
}

LinearBrowser::~LinearBrowser()
{
}

void LinearBrowser::setDisplayFont(QFont& font)
{
	mDictionary.setDisplayFont(font);
	ui.browseInfo->setFont(font);
}

void LinearBrowser::quickLookupDictionary(QString key)
{
	LOG<<"lookup \""<<key<<"\""<<endl;
	mDictionary.setLookupDelayMS(0);
	mDictionary.setInput(key);
	mDictionary.setLookupDelayMS(500);
	if (OPT.autoSpeak)
		mDictionary.speak(key);
}

void LinearBrowser::initBrowseTask()
{
	mWordManager.getBasicInfoByActiveState();
	mWordCount = mWordManager.getWordCount();
	if (mWordCount > 0)
	{
		mCurWordIndex = 0;
		ui.wordNavSlider->setRange(1, mWordCount);
		ui.wordNavSpinBox->setRange(1, mWordCount);
		updateControl();
	}
	else
		mCurWordIndex = -1;
}

void LinearBrowser::browseNext()
{
	if (mCurWordIndex < 0)
		return;

	mCurWordIndex = (++mCurWordIndex)%mWordCount;
	updateControl();
}

void LinearBrowser::browsePrevious()
{
	if (mCurWordIndex < 0)
		return;

	mCurWordIndex = (--mCurWordIndex + mWordCount)%mWordCount;
	updateControl();
}

void LinearBrowser::browseByOrder(int order)
{
	if (order >= 1 && order <= mWordCount)
	{
		if (mCurWordIndex != order - 1)
		{
			mCurWordIndex = order - 1;
			updateControl();
		}
	}
}

void LinearBrowser::updateControl()
{
	if (ui.wordNavSlider->value() != mCurWordIndex+1)
		ui.wordNavSlider->setValue(mCurWordIndex+1);

	if (ui.wordNavSpinBox->value() != mCurWordIndex+1)
		ui.wordNavSpinBox->setValue(mCurWordIndex+1);

	LOG<<"mCurWordIndex="<<mCurWordIndex<<endl;
	mWordManager.browseWordByIndex(mCurWordIndex);
	ui.browseInfo->setWordInfo(&mWordManager);
	quickLookupDictionary(mWordManager.getCurWord()->word);
}

void LinearBrowser::anchorClicked(const QUrl& url)
{
	QString url_s = url.toString();
	LOG<<"Anchor \""<<url_s<<"\" is clicked."<<endl;
	//LOG<<"url_s="<<url_s<<endl;
	QString type = url_s.left(url_s.indexOf(":"));
	type = type.simplified();
	QString content = url_s.right(url_s.size() - url_s.indexOf(":") - 1);
	content = content.simplified();
	if (type == "key")
		quickLookupDictionary(content);
	else
		LOG<<"Unexpected type: "<<type<<endl;
}

